<template>
  <div>
    <card title="个人资料" :width="1120">
      <div
        style="
          width: 100%;
          display: flex;
          flex-direction: column;
          align-items: center;
          padding-top: 40px;
          box-sizing: border-box;
        "
      >
        <div class="formbox pageForm">
          <el-form
            @submit.native.prevent
            ref="form"
            :model="user"
            :rules="rules"
            label-width="112px"
          >
            <el-form-item label="姓名" prop="nickName">
              {{ user.nickName }}
            </el-form-item>
            <el-form-item label="性别">
              <div>{{ user.sex == 1 ? '女' : '男' }}</div>
            </el-form-item>
            <el-form-item label="个人头像">
              <div
                class="userInfo-avatar-div"
                v-if="!user.avatar.includes('http')"
                :style="{ 'background-color': avatar }"
              >
                {{ getLastTwoCharacters(user.nickName) }}
              </div>
              <img
                v-if="user.avatar.includes('http')"
                :src="user.avatar"
                class="img-circle img-lg"
              />
            </el-form-item>
            <el-form-item label="学号" prop="userName">
              {{ user.studentNum }}
            </el-form-item>
            <el-form-item label="班级" prop="userName">
              {{ user.branchName }}—{{ user.majorName }}—{{ user.className }}
            </el-form-item>
            <div class="flex" style="margin: 40px 0 0 114px">
              <el-button
                style="margin-left: 32px"
                type="primary"
                @click="routeTo"
              >修改密码
              </el-button
              >
            </div>
          </el-form>
        </div>
      </div>
    </card>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import userAvatar from './userAvatar'
import { updateUserProfile } from '@/api/system/user'
import { studentInfo } from '@/api/mine/mine'
import { checkPhonenot, checkidCardnot } from '@/utils/validate'
import { getLastTwoCharacters } from '@/utils/util.js'

export default {
  name: 'Profile',
  components: { userAvatar },
  data() {
    return {
      getLastTwoCharacters: function(name) {
        return getLastTwoCharacters(name)
      },
      showEdit: true,
      user: {},
      // 表单校验
      rules: {
        phoneNumber: [
          { required: false, validator: checkPhonenot, trigger: 'blur' }
        ],
        idCard: [
          { required: false, validator: checkidCardnot, trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getInfo()
  },
  computed: {
    ...mapGetters(['avatar'])
  },
  methods: {
    routeTo() {
      this.$router.push({ path: '/user/reset' })
    },
    submit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: '正在处理…',
            spinner: 'el-icon-loading',
            background: 'rgba(255, 255, 255, 0.6)'
          })
          updateUserProfile(this.user).then(() => {
            loading.close()
            this.$modal.msgSuccess('修改成功')
            this.$store.commit('SET_AVATAR', this.user.avatar)
            this.$store.commit('SET_NAME', this.user.nickName)
            this.showEdit = true
          }).catch(() => {
            loading.close()
          })
        }
      })
    },
    /* 获取个人资料 */
    getInfo() {
      studentInfo().then((res) => {
        this.user = res.data
      })
    },
    cancel() {
      this.$router.go(-1)
    },
    AvatarReturn(e) {
      this.user.avatar = e
    }
  }
}
</script>
<style lang="scss" scoped>
.img-circle-div {
  border: 1px solid #eeeeee;
  width: 120px;
  height: 120px;
  border-radius: 60px;
  text-align: center;
  line-height: 118px;
  font-size: 60px;
  font-family: PingFangSC, PingFang SC, sans-serif;
  font-weight: 600;
  color: #ffffff;
}

.infobox {
  .title {
    padding: 32px 0 0 40px;
    font-size: 18px;
    font-family: PingFangSC-Semibold, PingFang SC, sans-serif;
    font-weight: 600;
    color: #333333;
    line-height: 25px;
  }

  .formbox {
    padding: 32px 0 0 211px;
  }

  .reset {
    margin-left: 18px;
    font-size: 16px;
    font-family: PingFangSC-Regular, PingFang SC, sans-serif;
    font-weight: 400;
    color: #28b28b;
  }
}

.userInfo-avatar-div {
  border: 1px solid #eeeeee;
  width: 120px;
  height: 120px;
  border-radius: 60px;
  text-align: center;
  line-height: 118px;
  font-size: 42px;
  font-family: PingFangSC, PingFang SC, sans-serif;
  font-weight: 600;
  color: #ffffff;
}
</style>
